package xupt.se.ttms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import xupt.se.ttms.idao.iOrderDAO;
import xupt.se.ttms.model.Order;
import xupt.se.util.MyDBUtil;

public class OrderDAO implements iOrderDAO{

	private final String insertSql="insert into T_Order(customer_id,ticket_id,order_price,order_date)values(?,?,?,?)";
	//通过主键id 删除
	private final String deleteSql="delete from T_Order where order_id=?";	
	
	private final String updateSql="update T_Order set customer_id=?,ticket_id=?,order_price=?,order_date=? where order_id=?";
	//通过主键id查询
	private final String selectSqlInt="select * from T_Order where order_id=?";
	
	//查询全部
	private final String selectSqlAll="select * from T_Order ";

	

	@Override
	public ResultSet execQuery( int i) throws Exception {
		String sql=selectSqlInt;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execQuery(sql, i);
		return rstSet;
	}

	@Override
	public ResultSet exexQueryAll() throws Exception {
		String sql=selectSqlAll;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execAllQuery(sql);
		return rstSet;
	}

	
	@Override
	public boolean execDelete( int i) throws Exception {
		String sql=deleteSql;
		boolean flag = false;
		MyDBUtil myDBUtil=new MyDBUtil();
		flag=myDBUtil.execDelete(sql, i);
		return flag;
	}
	@Override
	public List<Order> HandleOrderResultSet(ResultSet rs) throws SQLException {
		 //结果，在第一个查询结果有个rs标记，通过rs.next()遍历获取全部
		Order order=null;
		List<Order> orders=new ArrayList<>();
		 //获取结果的数据
        ResultSetMetaData rsmd = rs.getMetaData();
        //获取该条结果有多少个列数据
        int columnCount = rsmd.getColumnCount();
        
       
    	System.out.println("获取订票数据中");
    	//通过rs.next()遍历获取全部结果
        while (rs.next()) {
        
        	order=new Order();
       	 //输出每条结果的全部数据
           for (int i = 0; i < columnCount; i++) {

        	   order.setOrder_id(rs.getInt("order_id"));
        	   order.setCustomer_id(rs.getInt("customer_id"));
        	   order.setTicket_id(rs.getInt("ticket_id"));
        	   order.setOrder_price(rs.getInt("order_price"));
        	   order.setOrder_date(rs.getString("order_date"));
        	   
        	   
               System.out.print(rs.getString(i+1) + "\t");

           }
           orders.add(order);
           System.out.println();		                    
        }
    
        return orders;
     
	}

	@Override
	public boolean execInsert(Order order) throws Exception {
		String sql=insertSql;
		Connection conn=null;
		boolean flag=false;
		PreparedStatement ps=null;
		MyDBUtil db = new MyDBUtil();
		conn=db.openConnection();
		try {
			if(null==conn)
				throw new Exception("Database not connected!");
			
			ps=conn.prepareStatement(sql);
			//ps.setInt(1,customer.);
			
			ps.setInt(1,order.getCustomer_id());
			ps.setInt(2,order.getTicket_id());
			ps.setInt(3,order.getOrder_price());
			ps.setString(4,order.getOrder_date());

			flag=ps.execute();
			/*
			Statement stmt = conn.createStatement();
			flag = stmt.execute(sql);*/			
			ps.close();	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean execUpdate(Order order) throws Exception {
		String sql=updateSql;
		Connection conn=null;
		boolean flag=false;
		PreparedStatement ps=null;
		MyDBUtil db = new MyDBUtil();
		conn=db.openConnection();
		try {
			if(null==conn)
				throw new Exception("Database not connected!");
			
			ps=conn.prepareStatement(sql);
			//ps.setInt(1,customer.);
			ps.setInt(1,order.getCustomer_id());
			ps.setInt(2,order.getTicket_id());
			ps.setInt(3,order.getOrder_price());
			ps.setString(4,order.getOrder_date());
			ps.setInt(5, order.getOrder_id());
			
			flag=ps.execute();
			/*
			Statement stmt = conn.createStatement();
			flag = stmt.execute(sql);*/			
			ps.close();	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
}
